package link_list;

import java.util.Stack;

public class SingleLinkListDemo {
    public static void main(String[] args) {
        HeroNode h1 = new HeroNode(4,"张三","小三");
        HeroNode h2 = new HeroNode(2,"李四","小四");
        HeroNode h3 = new HeroNode(2,"王五","小五");
        HeroNode h4 = new HeroNode(1,"赵六","小六");
        HeroNode h5 = new HeroNode(1,"邓九","小九");

        SingleLinkedList sll = new SingleLinkedList();
//        sll.add(h1);
//        sll.add(h2);
//        sll.add(h3);
//        sll.add(h4);
        sll.addNo(h1);
        sll.addNo(h2);
        sll.addNo(h3);
        sll.addNo(h4);
//        sll.update(h5);
//       sll.del(h5);
//       sll.del(h1);
        sll.show();
        System.out.println(sll.count());
        //System.out.println(sll.getHeadBack(1));
        reverse(sll.getHead());
        sll.show();
        reversePrint(sll.getHead());
    }
    //反序打印
    public static void reversePrint(HeroNode head) {
        if(head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Stack<HeroNode> heroNodes = new Stack<>();
        HeroNode cur = head.next;
        while (cur != null) {
            heroNodes.push(cur);
            cur = cur.next;
        }
        while (heroNodes.size() > 0) {
            System.out.println(heroNodes.pop());
        }
    }
    //链表反转
    public static void reverse(HeroNode head) {
        if(head.next == null || head.next.next == null) {
            return;
        }
        HeroNode newHead = new HeroNode(0,"","");
        HeroNode temp = head.next;
        while (temp != null) {
            HeroNode cur = temp.next;
            temp.next = newHead.next;
            newHead.next = temp;
            temp = cur;

        }
        head.next = newHead.next;
    }
}

class SingleLinkedList {
    private HeroNode head;
    public SingleLinkedList() {
        this.head = new HeroNode(0, "", "");
    }
    public HeroNode getHead() {
        return head;
    }
    public void show() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head;
        while (temp.next != null) {
            System.out.println(temp.next);
            temp = temp.next;
        }
    }
    //获得链表中倒数第num个元素
    public HeroNode getHeadBack (int num) {
        HeroNode temp = head;
        boolean flag = false;
        int i = 1;
        while (true) {
            if(temp.next == null) {
                break;
            }else if(i == count()-(num-1)) {
                flag = true;
                break;
            }
            temp = temp.next;
            i++;
        }
        if (flag) {
            return temp.next;
        }else {
            System.out.println("查询不到");
            return head;
        }
    }
    //统计链表中的元素
    public int count() {
        int num = 0;
        HeroNode temp = head;
        while (temp.next != null) {
            num++;
            temp = temp.next;
        }
        return num;
    }
    //添加
    public void add(HeroNode node) {
        selectOpera(node,4);
    }
    //按顺序添加
    public void addNo(HeroNode node) {
        selectOpera(node,3);
    }
    //修改
    public void update(HeroNode node) {
        selectOpera(node,2);

    }
    //删除
    public void del(HeroNode node) {
        selectOpera(node,1);
    }
    public void selectOpera(HeroNode node,int select) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if(temp.next == null) {
                break;
            }else if(temp.next.getNo() == node.getNo()) {
                flag = true;
                break;
            }else if(temp.next.getNo() > node.getNo()) {
                break;
            }
            temp = temp.next;
        }
        switch (select) {
            case 1:{
                if(flag) {
                    HeroNode cur = temp.next;
                    temp.next = temp.next.next;
                    cur.next = null;
                }else {
                    System.out.println("未找到要删除的数据");
                }
                break;
            }
            case 2:{
                if(flag) {
                    temp.next.setName(node.getName());
                    temp.next.setNickName(node.getNickName());
                }else {
                    System.out.println("数据不存在");
                }
                break;
            }
            case 3:{
                if(flag) {
                    System.out.println("加入的数据已经存在");
                }else {
                    node.next = temp.next;
                    temp.next = node;
                }
                break;
            }
            case 4:{
                if(flag) {
                    System.out.println("加入的数据已存在");
                }else {
                    temp.next = node;
                }
                break;
            }
        }
    }
}
class HeroNode {
    private int no;
    private String name;
    private String nickName;
    public HeroNode next;

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }
    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName +"}";
    }
}

